import 'package:loxen/api/http_server/file_show.dart';
import 'package:loxen/api/http_server/inspiration.dart';
import 'package:loxen/api/http_server/user_follower.dart';
import 'package:loxen/mode/inspiration/inspiration_info_bean.dart';
import 'package:loxen/mode/inspiration/inspiration_page.dart';
import 'package:loxen/mode/master/master_index_bean.dart';
import 'package:loxen/mode/mine/user_follower_list.dart';
import 'package:rxdart/rxdart.dart';
import 'package:loxen/api/http_server/user_info.dart';
import 'package:loxen/mode/mine/mine_information_mode.dart';
import 'package:loxen/viewmode/base_view_model.dart';

class MineUserInfoViewModel extends BaseViewModel
    with UserInfoMixin, InspirationMixin, FileShowMixin, UserFollowerMixin {
  MineInformation user;
  FileShow fileShow;
  int pageSize = 3;
  InspirationPage inspPage;
  FollowerList followerList;

  void thisUserLike(Function(String msg) listener) {
    if (user.is_likes == 0) {
      userLike(user.user.userId).listen((event) {
        if (event == 201) {
          listener.call("点赞成功");
          user.is_likes = 1;
          notifyListeners();
        } else {
          listener.call("点赞失败");
        }
      }, onError: (error) {
        print(error);
        listener.call("网络异常");
      });
    } else {
      unUserLike(user.user.userId).listen((event) {
        if (event == 200) {
          listener.call("取消点赞成功");
          user.is_likes = 0;
          notifyListeners();
        } else {
          listener.call("取消点赞失败");
        }
      }, onError: (error) {
        print(error);
        listener.call("网络异常");
      });
    }
  }

  void unFollower({Function(String error) onError}) {
    unFollowerUser(user.user.userId.toString()).listen((event) {
      if (event == 200) {
        user.is_follow = 0;
        notifyListeners();
      } else {
        if (onError != null) onError.call('取消关注失败');
      }
    }, onError: (error, stack) {
      print(error);
      print(stack);
      if (onError != null) onError.call('网络异常');
    });
  }

  void follower({Function(String error) onError}) {
    followerUser(user.user.userId.toString()).listen((event) {
      if (event == 201) {
        user.is_follow = 1;
        notifyListeners();
      } else {
        if (onError != null) onError.call('关注失败');
      }
    }, onError: (error, stack) {
      print(error);
      print(stack);
      if (onError != null) onError.call('网络异常');
    });
  }

  Future<List<Follower>> nextFollower(String userId, int page) async {
    var data = await userFollowerList(
            userId: userId,
            page: page.toString(),
            pageSize: pageSize.toString())
        .first;
    followerList = data;
    return data.results;
  }

  Future<List<InspirationInfo>> nextInspiration(int page) async {
    var data = await userInspirationPage(
            userId: user.id.toString(),
            page: (page + 1).toString(),
            pageSize: pageSize.toString())
        .flatMap((value) => Stream.value(value.results))
        .first;
    return data;
  }

  void loadData(String userId) {
    if (mState == BaseState.LOADING) return;
    Rx.zip2<FileShow, MineInformation, bool>(
        showFile(userId: userId), userInfo(id: userId), (a, b) {
      fileShow = a;
      if (b != null) {
        user = b;
        return true;
      }
      return false;
    }).doOnListen(() {
      mState = BaseState.LOADING;
      notifyListeners();
    }).listen((event) {
      if (event != null) {
        mState = BaseState.CONTENT;
      } else {
        mState = BaseState.EMPTY;
      }
    }, onError: (error) {
      print(error);
      mState = BaseState.FAIL;
    }, onDone: () => notifyListeners());
  }
}
